package com.tiny.mirror.service.product.support.strategy.lhc;

import com.tiny.mirror.basic.common.code.CommonStatusCode;
import com.tiny.mirror.basic.web.exception.BusinessException;
import com.tiny.mirror.service.product.domain.ability.LhcParams.LhcItemParams;
import com.tiny.mirror.service.product.domain.ability.LhcParams.LhcPlayParams;
import com.tiny.mirror.service.product.support.strategy.lhc.dto.LhcNumberDTO;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

public abstract class AbstractLhcStrategy implements LhcStrategy {

  @Override
  public BigDecimal getReward(LhcPlayParams lhcPlayParams, BigDecimal amount) {
    BigDecimal rewardPercent = lhcPlayParams.getRewardPercent();
    return rewardPercent == null ? BigDecimal.ZERO
        : amount.multiply(lhcPlayParams.getRewardPercent())
            .divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP);
  }

  // 检查购买项是否符合条件
  public void checkAmount(BigDecimal amount) {
    // 验证金额
    if (BigDecimal.ZERO.compareTo(amount) >= 0) {
      throw new BusinessException(CommonStatusCode.COMMON_ERROR);
    }
  }

  // 检查购买项是否符合条件
  public abstract void checkItem(Integer limit, String... item);

  // 单个匹配计算
  public BigDecimal match(
      LhcPlayParams lhcPlayParams, BigDecimal amount, LhcNumberDTO result, String item
  ) {
    List<LhcItemParams> itemParamsList = lhcPlayParams.getItemList();
    String luckyNum = String.valueOf(result.getLuckyNum());
    BigDecimal odds = itemParamsList.stream()
        .filter(i -> i.getItem().equals(luckyNum))
        .map(i -> i.getOdds()).findFirst().get();
    if (luckyNum.equals(item)) {
      return amount.multiply(odds);
    } else {
      return BigDecimal.ZERO;
    }
  }
}
